/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.checkstyle;

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;

import javax.swing.*;
import java.awt.*;

public class CheckStyleOptionsPanel extends JPanel {
    private TextFieldWithBrowseButton prodConfigFileField;
    private TextFieldWithBrowseButton testConfigFileField;

    public CheckStyleOptionsPanel(CheckStyleConfiguration config) {
        super(new BorderLayout(5, 5));
        Box box = Box.createHorizontalBox();
        Box labels = Box.createVerticalBox();
        Box fields = Box.createVerticalBox();
        labels.add(new JLabel("Source Config File"));
        labels.add(new JLabel("Test Config File"));
        prodConfigFileField = new TextFieldWithBrowseButton();
        prodConfigFileField.addBrowseFolderListener("Choose Checkstyle File", "Choose a Checkstyle XML configuration file to use for Source folders for this module.", null, FileChooserDescriptorFactory.createXmlDescriptor());
        prodConfigFileField.setText(config.PRODUCTION_CONFIG);
        prodConfigFileField.setTextFieldPreferredWidth(30);
        fields.add(prodConfigFileField);
        testConfigFileField = new TextFieldWithBrowseButton();
        testConfigFileField.setText(config.TEST_CONFIG);
        testConfigFileField.addBrowseFolderListener("Choose Checkstyle File", "Choose a Checkstyle XML configuration file to use for Test Sources folders for this module.", null, FileChooserDescriptorFactory.createXmlDescriptor());
        testConfigFileField.setTextFieldPreferredWidth(30);
        fields.add(testConfigFileField);
        box.add(labels);
        box.add(fields);
        add(box, BorderLayout.NORTH);

    }

    public void reset(CheckStyleConfiguration config) {
        prodConfigFileField.setText(config.PRODUCTION_CONFIG);
        testConfigFileField.setText(config.TEST_CONFIG);
    }

    public void save(CheckStyleConfiguration config) {
        config.PRODUCTION_CONFIG = prodConfigFileField.getText();
        config.TEST_CONFIG = testConfigFileField.getText();
    }

    public boolean isModified(CheckStyleConfiguration config) {
        return !prodConfigFileField.getText().equals(config.PRODUCTION_CONFIG) ||
                !testConfigFileField.getText().equals(config.TEST_CONFIG);
    }
}
